package com.util.mail;

import java.io.File;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

public class TestSpringMail {
	/**
	 * 发送文本邮件
	 */
	@Ignore
	@Test
	public void testSimpleMail(){
		//邮件发送器
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		senderImpl.setHost("smtp.qq.com");
		senderImpl.setUsername("754743565@qq.com");
		senderImpl.setPassword("smtp754743565");
		
		Properties prop = new Properties();
		prop.put("mail.smtp.auth", "true");
		prop.put("mail.smtp.timeout", "25000");
		senderImpl.setJavaMailProperties(prop);
		
		//邮件消息对象
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		//发送多个邮件
		//String[] array = new String[]{"754743565@qq.com","1523692933@qq.com"};
		//mailMessage.setTo(array);
		mailMessage.setTo("1523692933@qq.com");
		mailMessage.setFrom("754743565@qq.com");
		mailMessage.setSubject("测试主题");
		mailMessage.setText("测试内容。。。。。。。。。。。。。。。。");
		
		//发送邮件
		senderImpl.send(mailMessage);
		
		System.out.println("发送成功");
	}
	/**
	 * 发送简单html邮件
	 * @throws MessagingException 
	 */
	@Ignore
	@Test
	public void testHtmlMail() throws MessagingException{
		//邮件发送器
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		senderImpl.setHost("smtp.qq.com");
		senderImpl.setUsername("754743565@qq.com");
		senderImpl.setPassword("smtp754743565");
		
		Properties prop = new Properties();
		prop.put("mail.smtp.auth", "true");
		prop.put("mail.smtp.timeout", "25000");
		senderImpl.setJavaMailProperties(prop);
		
		//邮件消息对象
		MimeMessage mailMessage = senderImpl.createMimeMessage();

		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
		messageHelper.setTo("1523692933@qq.com");
		messageHelper.setFrom("754743565@qq.com");
		messageHelper.setSubject("测试html标题");
		messageHelper.setText("<html><head></head><body><h1>hello!!spring html<br/>text </h1></body></html>",true);
		
		//发送邮件
		senderImpl.send(mailMessage);
		
		System.out.println("发送成功");
	}
	/**
	 * 发送图片邮件
	 * @throws MessagingException 
	 */
	@Test
	public void testImgMail() throws MessagingException{
		//邮件发送器
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		senderImpl.setHost("smtp.qq.com");
		senderImpl.setUsername("754743565@qq.com");
		senderImpl.setPassword("smtp754743565");
		
		Properties prop = new Properties();
		prop.put("mail.smtp.auth", "true");
		prop.put("mail.smtp.timeout", "25000");
		senderImpl.setJavaMailProperties(prop);
		
		//邮件消息对象
		MimeMessage mailMessage = senderImpl.createMimeMessage();

		MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true);//等于真，才能嵌套图片
		messageHelper.setTo("1523692933@qq.com");
		messageHelper.setFrom("754743565@qq.com");
		messageHelper.setSubject("测试html标题");
		messageHelper.setText(
				"<html><head></head><body><h1>hello!!spring html<br/>text </h1>"
				+ "<img src=\"cid:aaa\"></body></html>",
				true);
		
		FileSystemResource img = new FileSystemResource(new File("D:/Documents/Pictures/0.jpg"));
		messageHelper.addInline("aaa", img);
		//发送邮件
		senderImpl.send(mailMessage);
		
		System.out.println("发送成功");
	}
}
